#include <xp/lnx/iptc.h>
#include <xp/bas/time.h>
#include <xp/bas/stdio.h>

#ifdef __linux
#include <mcheck.h>
#endif

int xp_main (int argc, xp_char_t* argv[])
{

	xp_iptc_t iptc;

#ifdef __linux
	mtrace ();
#endif

	if (argc != 3) {
		xp_printf (XP_TEXT("usage: %s table chain\n"), argv[0]);
		return -1;
	}

	if (xp_iptc_open (&iptc, argv[1]) == XP_NULL) {
		xp_printf (XP_TEXT("cannot open table - %s\n"), argv[1]);
		return -1;
	}

/*
	if (xp_iptc_create_chain (&iptc, argv[2]) == -1) {
		xp_printf (XP_TEXT("warning: cannot create chain - %s\n"), argv[2]);
	}
*/

	{
		unsigned char mac[6] = {
			0xFF, 0xAA, 0x01, 0x05, 0x07, 0x09
		};

	/*
	if (xp_iptc_insert_entry (
		&iptc, argv[2], XP_TEXT("REJECT"),
		xp_false, IPPROTO_TCP, 
		xp_false, 0x12345678, 0x00FFFFFF, 
		xp_false, 0x87654321, 0x00FFFFFF, 
		xp_true,  XP_TEXT("eth1"),
		xp_false,  XP_TEXT("eth2"),
		xp_false, 0, 65535, 
		xp_true, 110, 110,
		1, mac, 
		0, 0, 0,
		600, 600, 0x11, IPT_ICMP_HOST_UNREACHABLE) == -1) {
		xp_printf (XP_TEXT("cannot add an entry\n"));
		return -1;
	}
	*/
	/*
	if (xp_iptc_append_entry (
                &iptc,
		argv[2],
                XP_TEXT("SNAT"),
                xp_false, 0,
                xp_false, 0, 0,
                xp_false, 0, 0,
                xp_false, XP_NULL,
                xp_false, XP_NULL,
                xp_false, 0, 0,
                xp_false, 0, 0,
                1, mac,
                0, 0, 0,
                0, 0, 0,
                0, 0,
                0, 0,
                0, 0) == -1) {
		xp_printf (XP_TEXT("cannot add an entry\n"));
		return -1;
	}
	*/

/*
	if (xp_iptc_append_entry (
                &iptc,
		argv[2],
                XP_TEXT("SNAT"),
                xp_false, 0,
                xp_false, 0, 0,
                xp_false, 0, 0,
                xp_false, XP_NULL,
                xp_false, XP_TEXt("eth1"),
                xp_false, 0, 0,
                xp_false, 0, 0,
                0, XP_NULL,
                0, 0, 0,
                0, 0, 0,
                0, 0,
                0, 0,
                inet_addr("192.168.123.81"), inet_addr("192.168.123.81")) == -1) {
		xp_printf (XP_TEXT("cannot add an entry\n"));
		return -1;
	}
*/

	if (xp_iptc_insert_entry (
                &iptc,
		argv[2], 0,
                XP_TEXT("DNAT"), 
                xp_false, IPPROTO_TCP,
                xp_false, 0, 0,
                xp_false, 0, 0,
                //xp_false, XP_TEXT("eth1"),
                xp_false, XP_NULL,
                xp_false, XP_NULL,
                xp_false, 0, 0xFFFFu,
                xp_false, 999, 999,
                0, XP_NULL,
                0, 0, 0,
                0, 0, 0,
                998, 998,
                0, 0,
                inet_addr("192.168.123.87"), inet_addr("192.168.123.87")) == -1) {
		xp_printf (XP_TEXT("cannot add an entry\n"));
		return -1;
	}

	}

	if (xp_iptc_commit (&iptc) == -1) {
		xp_printf (XP_TEXT("commit failed\n"));
	}
	xp_iptc_close (&iptc);
#ifdef __linux
	muntrace ();
#endif
	return 0;
}
